/**
	*分享文案的 ShareId(dataKey) 生成规则:https://git.code.oa.com/CCTVVideo/CCTVServer/wikis/%E5%88%86%E4%BA%AB%E6%96%87%E6%A1%88
*/
import getScript from './getScript'

let cacheShare = null
let cachePm = null
export function getShareJs () {
	if (!cachePm) {
		cachePm = new Promise(resolve => {
			if (cacheShare) {
				resolve(cacheShare)
			} else {
				getScript({
					src: '//s.yangshipin.cn/CCTVVideo/cctvh5-share/cctvh5-share.min.js',
					onLoad () {
						cacheShare = window['cctvh5-share'].default
						resolve(cacheShare)
					}
				})
			}
		})
	}
	return cachePm
}

const Share = {}

const methods = [
	'init',
	'setShareData',
	'invokeCCTVShare',
	'invokeBrowserGuide'
]

methods.forEach(name => {
	Share[name] = async (option) => {
		let $share = await getShareJs()
		let rs = null
		if (typeof $share[name] === 'function') {
			rs = await $share[name](option)
		}
		return rs
	}
})

export default Share
